<?php
/**
* @file $Id: miscExport.fnc.php 580 2007-06-05 19:19:11Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function _makeNextSchool($value,$column)
{	global $THIS_RET,$_FOCUS;

	if($value=='0')
		return _('Retain');
	elseif($value=='-1')
		return _('Do not enroll after this school year');
	elseif($value==$THIS_RET['SCHOOL_ID'])
		return _('Next Grade at ').GetSchool($value);
	else
		return GetSchool($value);
}

function _makeCalendar($value,$column)
{	global $THIS_RET,$_FOCUS,$calendars_RET;

	if(!$calendars_RET)
		$calendars_RET = DBGet(DBQuery("SELECT CALENDAR_ID,DEFAULT_CALENDAR,TITLE FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."'"),array(),array('CALENDAR_ID'));

	return $calendars_RET[$value][1]['TITLE'];
}

function _makeContactInfo($value,$column)
{	global $THIS_RET,$contacts_RET,$elems;

	$column = explode('_',strtolower($column));
	if($THIS_RET['ADDRESS_ID'])
		$record = $contacts_RET[$THIS_RET['STUDENT_ID']][$THIS_RET['ADDRESS_ID']];
	else
		$record = $contacts_RET[$THIS_RET['STUDENT_ID']];

	if($record[$column[0]])
	{
		if($column[1]=='name')
			return $record[$column[0]][1]['NAME'];
		if($column[1]=='home')
			return $record[$column[0]][1]['PHONE'];
			
		foreach($record[$column[0]] as $info)
		{
			if(strpos($info['TITLE'],$column[1])===0)
				return $info['VALUE'];
		}
		// we should only reach here if the parent exists, but the contact info requested doesn't
		return;
	}
	// there isn't a contact called Father / Dad ... so, return any contact called parent instead
	if(($column[0]=='father' || $column[0]=='mother'))
	{
		foreach($record['parent'] as $i=>$info)
		{
			$return = false;
			if($column[1]=='name')
				$return = $record['parent'][$i]['NAME'];
			if($column[1]=='home')
				$return = $record['parent'][$i]['PHONE'];

			if(strpos($info['TITLE'],$column[1])===0)
				$return = $info['VALUE'];

			if($return)
			{
				// this contact has already been used by the other parent ... 
				if(($THIS_RET[$column[0]] && $THIS_RET[$column[0]]!=$info['PERSON_ID']) || ($column[0]=='father' && $THIS_RET['mother']==$info['PERSON_ID']) || ($column[0]=='mother' && $THIS_RET['father']==$info['PERSON_ID']))
					continue;
				// set the person_id of the contact for this parent (either mother or father) ... this will be reset each time $THIS_RET is reset
				$THIS_RET[$column[0]] = $info['PERSON_ID'];
				return $return;
			}
		}

		// the parent doesn't exist, or the contact info requested doesn't
		return;
	}
	// this is the 4th contact or there isn't a contact that starts with Emergency ... so, return any other contact instead
	if($column[0]=='emergency' || $column[0]=='contact')
	{
		foreach($record['other'] as $i=>$info)
		{
			$return = false;
			if($column[1]=='name')
				$return = $record['other'][$i]['NAME'];
			if($column[1]=='home')
				$return = $record['other'][$i]['PHONE'];

			if(strpos($info['TITLE'],$column[1])===0)
				$return = $info['VALUE'];
			if($return)
			{
				// this contact has already been used by the other contact ... 
				if(($THIS_RET[$column[0]] && $THIS_RET[$column[0]]!=$info['PERSON_ID']) || ($column[0]=='emergency' && $THIS_RET['contact']==$info['PERSON_ID']) || ($column[0]=='contact' && $THIS_RET['emergency']==$info['PERSON_ID']))
					continue;
				// set the person_id of the contact for this parent (either emergency or other) ... this will be reset each time $THIS_RET is reset
				$THIS_RET[$column[0]] = $info['PERSON_ID'];
				return $return;
			}
		}

		// the contact doesn't exist, or the contact info requested doesn't
		return;
	}

	// we should never reach this point
	return ;
}

function _runCalc($value,$column)
{	global $row,$_FOCUS,$num,$_runCalc_num;

	if(!isset($num))
		$num = 0;
	if(!isset($_runCalc_num))
		$_runCalc_num = $num;

	$_runCalc_start_REQUEST = $_REQUEST;
	$row++;
	if(!$_FOCUS['Calc'.substr($column,12)])
	{
		$url_RET = DBGet(DBQuery("SELECT URL FROM SAVED_CALCULATIONS WHERE ID='".substr($column,12)."'"));
		$url = $url_RET[1]['URL'];
		$url = urldecode($url);
		$vars = substr($url,(strpos($url,'?')+1));
		$modname = substr($url,0,strpos($url,'?'));
		
		$vars = str_replace('&amp;','&',$vars);
		$vars = explode('&',$vars);
		$_REQUEST = array();
		foreach($vars as $code)
		{
			$equals = strpos($code,'=');
	
			if(strpos($code,'[')!==false)
				$code = "\$_REQUEST[".ereg_replace('([^]])\[','\1][',substr($code,0,$equals))."='".substr($code,$equals+1)."';";
			else
				$code = "\$_REQUEST['".substr($code,0,$equals)."']='".substr($code,$equals+1)."';";
			eval($code);
		}
		$_FOCUS['Calc'.substr($column,12)] = $_REQUEST;
	}
	else
		$_REQUEST = $_FOCUS['Calc'.substr($column,12)];

	for($i=1;$i<=15;$i++)
	{
		$_REQUEST['screen'][$i]['stuid'] = $value;
	}
	$num = $_runCalc_num;
	// so Calculations.php doesn't include the functions within this function
	$_REQUEST['modfunc'] = 'misc/Export.php';
	$_REQUEST['modfunc'] = 'echoXMLHttpRequest';
	$return = include("modules/Reports/Calculations.php");
	$_REQUEST = $_runCalc_start_REQUEST;
	
	if(!isset($_FOCUS['_runCalc_max'][$column]) || $_FOCUS['_runCalc_max'][$column]<$return)
		$_FOCUS['_runCalc_max'][$column] = $return;
	
	return $return;
}

function _createGraphs(& $RET)
{	global $_FOCUS,$calculations_RET;

	$last = count($RET);
	for($i=1;$i<=$last;$i++)
	{
		foreach($calculations_RET as $calc)
		{
			if($_REQUEST['fields']['GRAPH_'.$calc['ID']]=='Y')
			{
				$scale = (100/$_FOCUS['_runCalc_max']['CALCULATION_'.$calc['ID']]);
				$RET[$i]['GRAPH_'.$calc['ID']] = '<!--' . ((int) ($RET[$i]['CALCULATION_'.$calc['ID']]*$scale)) . '--><IMG SRC=assets/pixel_grey.gif class=innerLO_field width=' . ((int) ($RET[$i]['CALCULATION_'.$calc['ID']]*$scale)) . ' height=10> '.$RET[$i]['CALCULATION_'.$calc['ID']];
			}
		}
	}
}

function _makeTextInput($value,$column)
{	global $THIS_RET;

	return TextInput($value,'students['.$THIS_RET['STUDENT_ID'].']['.$column.']','','','force');
}

function _makeSelectInput($value,$column)
{	global $THIS_RET,$fields_RET;

	$select_options = str_replace("\n","\r",str_replace("\r\n","\r",$fields_RET[substr($column,7)][1]['SELECT_OPTIONS']));
	$select_options = explode("\r",$select_options);
	if(count($select_options))
	{
		foreach($select_options as $option)
			$options[$option] = $option;
	}

	$extra = 'style="max-width:250;"';
	return SelectInput($value,'students['.$THIS_RET['STUDENT_ID'].']['.$column.']','',$options,_('N/A'),$extra,'force');
}

function _makeCheckboxInput($value,$column)
{	global $THIS_RET,$fields_RET;

	return CheckboxInput($value,'students['.$THIS_RET['STUDENT_ID'].']['.$column.']','','',false);
}

function _getEnrollmentCode($id,$column='TITLE')
{	global $_FOCUS;
	
	if($column!='TITLE' && $column!='SHORT_NAME' && $column!='TYPE')
		$column = 'TITLE';

	if(!$_FOCUS['_getEnrollmentCode'])
	{
		$QI=DBQuery("SELECT ID,TITLE,SHORT_NAME,TYPE FROM STUDENT_ENROLLMENT_CODES WHERE SYEAR='".UserSyear()."'");
		$_FOCUS['_getEnrollmentCode'] = DBGet($QI,array(),array('ID'));
	}

	return $_FOCUS['_getEnrollmentCode'][$id][1][$column];
}
?>